<template>
  <!-- 放置一个弹层 用来编辑新增节点 -->
  <el-dialog :title="`${formData.id?'编辑':'新增'}权限点`" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="formData.description" style="width:90%" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getPermissionById, editPermission, addPermission } from '@/api/permission'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '1'
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '权限描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async btnOK() {
      await this.$refs.perForm.validate()
      if (this.formData.id) {
        await editPermission(this.formData)
        this.$message.success('编辑成功')
      } else {
        await addPermission(this.formData)
        this.$message.success('新增成功')
      }
      this.$emit('update:showDialog', false)
      this.$emit('updatePermission')
    },
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '1'
      }
      this.$refs.perForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getPermissionById(id) {
      this.formData = await getPermissionById(id)
    },
    async addPermission(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
    }
  }
}
</script>

<style>

</style>
